package bairui.international.console.web.controller.user.user;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.lianjing.sh.project.commons.util.Util_FileDeal;
import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.sh_lianjing.util.Util_Stream;

import bairui.international.common.config.Config_Path;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.service.Service_User;

/**
 *
 * 生成二维码大图
 *
 * @generated
 * 
 * @see STest_C_User_User_Generate_FileDownload 测试
 *
 */
@Controller
public class C_User_User_Generate_FileDownload
 extends SC_User_User_Generate_FileDownload 
{
	@Autowired private Service_User service_User;
	@Autowired private Config_Path config_Path;
//	@Autowired private Config_QR config_QR;
	
	/**
	 * 下载文件
	 */
	@Action(FileDownload.class)
	@GetMapping(FileDownload.RequestMappingValue)
	public void fileDownload(
		HttpServletRequest request,
		HttpServletResponse response, 
		@PathVariable(name=FileDownload.PathVariableName.Id) long id
	) throws Exception {
		TB_User user = service_User.findById(id);
		//
		Util_FileDeal.downloadFile(request, response, "二维码.jpg", ContentType.IMAGE_JPEG);
//		// 生成二维码
//    	new GenerateQRCode().toStream(
//    			new Config() {
//    				@Override
//    				public byte[] getLogoByteArray() {
//    					return getImageFromNetByUrl(user.getAvatarUrl());
//    				}
//    				@Override public int getWidth() {
//    					return 5000;
//    				}
//    				@Override public int getHeight() {
//    					return 5000;
//    				}
//    				@Override public String getImageFormat() {
//    					return "jpeg";
//    				}
//    				@Override public int getLogoMarginWidth() {
//    					return 100;
//    				}
//    				@Override public int getLogoBorderWidth() {
//    					return 20;
//    				}
//    			},
//    			config_QR.getWx_qr_content_domain() + ":"+ config_Path.getService_img_port() +"/qr/user" + "?id=" + user.getId(), 
//    			response.getOutputStream()
//    		);
	}
	
	public static byte[] getImageFromNetByUrl(String strUrl) {
	    try {
	        HttpURLConnection conn = (HttpURLConnection) new URL(strUrl).openConnection();
		        conn.setRequestMethod("GET");
		        conn.setConnectTimeout(5 * 1000);
		    try(InputStream inStream = conn.getInputStream()){
		    	return Util_Stream.copyToByteArray(inStream);
		    }
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return null;
	}

}
